Imports System.Collections.ObjectModel
Imports System.IO
Imports System.Xml

Public Class ResponseCacheForm

    Public Sub Add(ByVal req As Request, ByVal resp As Response)

        If TypeOf resp Is SessionResponse Then
            Dim sr As SessionResponse = resp
            With TreeView1.Nodes
                '.Add(sr.SessionId, "Session " & .Count + 1 & " [" & Now & "]").Name = sr.SessionId
                .Add(sr.SessionId, sr.SessionId)
            End With
        ElseIf TypeOf resp Is InformationResponse Then
            Dim iresp As InformationResponse = resp
            Dim ireq As InformationRequest = req
            Dim sessionId As String = ireq.SessionId
            Dim tool As String = ireq.Tool
            Dim modality As String = ireq.Modality
            Dim model As String = ireq.Model
            Dim lang As String = ireq.Language
            Dim keys As List(Of String) = ireq.Keys
            Dim tn As TreeNode
            Dim tnX As TreeNode

            tn = TreeView1.Nodes(sessionId)
            If tn Is Nothing Then
                tn = TreeView1.Nodes.Add(sessionId, sessionId)
            End If

            tnX = tn.Nodes(tool)
            If tnX Is Nothing Then
                tn = tn.Nodes.Add(tool, tool)
            Else
                tn = tnX
            End If

            tnX = tn.Nodes(modality)
            If tnX Is Nothing Then
                tn = tn.Nodes.Add(modality, modality)
            Else
                tn = tnX
            End If

            tnX = tn.Nodes(model)
            If tnX Is Nothing Then
                tn = tn.Nodes.Add(model, model)
            Else
                tn = tnX
            End If

            tnX = tn.Nodes(lang)
            If tnX Is Nothing Then
                tn = tn.Nodes.Add(lang, lang)
            Else
                tn = tnX
            End If

            For Each key As String In keys
                tnX = tn.Nodes(key)
                If tnX Is Nothing Then
                    tn = tn.Nodes.Add(key, key)
                Else
                    tn = tnX
                End If
            Next
            tn.Tag = iresp.InfoList
        ElseIf TypeOf resp Is FileResponse Then
            Dim fresp As FileResponse = resp

            SaveFileDialog1.FileName = fresp.Filename
            If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                Dim savepath As String = SaveFileDialog1.FileName
                fresp.Save(savepath)
            Else
                fresp.Close()
            End If
        Else
            'MsgBox(resp.StatusCode)
        End If
    End Sub

    Private Sub Session_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        If TypeOf Owner Is RequestBuilderForm Then
            Dim dc As RequestBuilderForm = Owner

            If e.CloseReason = CloseReason.UserClosing Then
                e.Cancel = True

                Hide()
                dc.chkShowResponse.Checked = False
            End If
        End If
    End Sub

    Private Sub TreeView1_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect

        ListView1.Items.Clear()

        Dim tn As TreeNode = e.Node
        If tn.Parent Is Nothing Then
            btnAccept.Enabled = True
        ElseIf tn.Nodes.Count = 0 Then

            Dim infoList As ReadOnlyCollection(Of InstallInformation) = tn.Tag
            For Each info As InstallInformation In infoList
                With ListView1.Columns
                    For i As Integer = .Count - 6 To info.Keys.Count - (.Count - 6)
                        .Add("key " & i)
                    Next
                End With
                With ListView1.Items.Add(info.Identifier).SubItems
                    .Add(info.Version)
                    .Add(info.DescriptionLanguage)
                    .Add(info.Description)
                    .Add(info.CheckValueType)
                    .Add(info.CheckValue)
                    .Add(info.Tool)
                    For Each key As String In info.Keys
                        .Add(key)
                    Next
                End With
            Next
            'btnAccept.Enabled = True
        Else
            btnAccept.Enabled = False
        End If
    End Sub

    Private Sub TreeView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.DoubleClick

        TreeExit()

    End Sub

    Private Sub TreeView1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TreeView1.KeyPress

        If e.KeyChar = vbLf Then
            TreeExit()
        End If
    End Sub

    Private Sub Session_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        With ListView1.Columns
            .Add("identifier")
            .Add("version")
            .Add("desc lang")
            .Add("description")
            .Add("cv type")
            .Add("check value")
            .Add("tool")
        End With
    End Sub

    Private Sub ListView1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ListView1.KeyPress

        Select Case e.KeyChar
            Case vbCr
                ListExit1()
            Case vbLf
                ListExit2()
        End Select
    End Sub

    Private Sub ListExit1()

        If TypeOf Owner Is RequestBuilderForm Then
            Dim dc As RequestBuilderForm = Owner

            If ListView1.SelectedItems.Count > 0 Then
                Dim item As ListViewItem = ListView1.SelectedItems(0)
                dc.optFile.Focus()
                dc.optFile.Checked = True
                dc.txtIdentifier.Focus()
                dc.txtIdentifier.Text = item.Text

                Hide()
                dc.chkShowResponse.Checked = False
            End If
        End If
    End Sub

    Private Sub ListExit2()

        If TypeOf Owner Is RequestBuilderForm Then
            Dim dc As RequestBuilderForm = Owner

            If ListView1.SelectedItems.Count > 0 Then
                Dim item As ListViewItem = ListView1.SelectedItems(0)
                dc.optFile.Focus()
                dc.optFile.Checked = True
                dc.txtIdentifier.Focus()
                dc.txtIdentifier.Text = item.SubItems(5).Text

                Hide()
                dc.chkShowResponse.Checked = False
            End If
        End If
    End Sub

    Private Sub TreeExit()

        If TypeOf Owner Is RequestBuilderForm Then
            Dim dc As RequestBuilderForm = Owner

            Dim tn As TreeNode = TreeView1.SelectedNode
            If tn IsNot Nothing AndAlso tn.Parent Is Nothing Then
                dc.optInformation.Focus()
                dc.optInformation.Checked = True
                dc.txtSessionId.Focus()
                dc.txtSessionId.Text = tn.Text
                dc.txtLanguage.Focus()

                Hide()
                dc.chkShowResponse.Checked = False
            End If
        End If
    End Sub

    Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click

        Dim xml As String = "<!DOCTYPE request PUBLIC ""pub"" ""xyz""><request/>"
        Dim tr As New StringReader(xml)

        Dim settings As New XmlReaderSettings
        settings.ProhibitDtd = False
        settings.ValidationType = ValidationType.DTD
        settings.XmlResolver = New Dtd("request.dtd", "request")

        Dim xr As XmlReader = XmlReader.Create(tr, settings)
        While xr.Read
        End While
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click

        If TypeOf Owner Is RequestBuilderForm Then
            Dim dc As RequestBuilderForm = Owner

            Hide()
            dc.chkShowResponse.Checked = False
        End If
    End Sub

    Private Sub btnTakeIdentifier_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTakeIdentifier.Click

        ListExit1()

    End Sub

    Private Sub btnTakeCheckValue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTakeCheckValue.Click

        ListExit2()

    End Sub

    Private Sub ListView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged

        btnTakeCheckValue.Enabled = True
        btnTakeIdentifier.Enabled = True

    End Sub
End Class